package com.xinchao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinchao.core.constant.DeviceModelConstant;
import com.xinchao.core.constant.ModuleConstant;
import com.xinchao.core.constant.PageConstant;
import com.xinchao.core.exception.ServiceException;
import com.xinchao.dao.DeviceModelMapper;
import com.xinchao.model.DeviceModel;
import com.xinchao.model.dto.DeviceModelDTO;
import com.xinchao.model.page.PageInfo;
import com.xinchao.model.vo.DeviceModelExportListVO;
import com.xinchao.model.vo.DeviceModelListVO;
import com.xinchao.model.vo.DeviceModelSelectListVO;
import com.xinchao.service.DeviceModelService;
import com.xinchao.service.OperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 终端机型
 *
 * @author linqiang
 * @date 2018-08-07 14:47
 */
@Service(value = "deviceModelService")
public class DeviceModelServiceImpl implements DeviceModelService {
    @Autowired
    private DeviceModelMapper deviceModelMapper;
    @Autowired
    private OperationLogService operationLogService;

    /**
     * 获取终端型号分页列表
     *
     * @param pageNum  当前页
     * @param pageSize 每页条数
     * @return Page<DeviceModelListVO>
     */
    @Override
    public PageInfo<DeviceModelListVO> listDeviceModelByPage(Integer pageNum, Integer pageSize,String deviceModel,String mainboard,String brand,Boolean isExits4G) {
        pageNum = pageNum == null ? PageConstant.DEFAULT_PAGE_NUM : pageNum;
        pageSize = pageSize == null ? PageConstant.DEFAULT_PAGE_SIZE : pageSize;
        PageHelper.startPage(pageNum, pageSize);
        Page page = deviceModelMapper.listDeviceModelByPage(deviceModel,mainboard,brand,isExits4G);
        PageInfo<DeviceModelListVO> pageInfo = new PageInfo<>();
        if (page == null) {
            return pageInfo;
        }
        pageInfo.setList(page.getResult());
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPageNum(page.getPageNum());
        pageInfo.setPageSize(page.getPageSize());
        return pageInfo;
    }

    /**
     * 通过型号名称查找终端机型信息
     *
     * @param deviceModel 型号名称
     * @return DeviceModel
     */
    @Override
    public DeviceModel getDeviceModelByName(String deviceModel) {
        return deviceModelMapper.getDeviceModelByName(deviceModel);
    }

    /**
     * 新增终端机型
     *  @param deviceModel 新增的机型
     * @param request     请求
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDeviceModel(DeviceModel deviceModel, HttpServletRequest request) {
        deviceModel.setAddTime(System.currentTimeMillis());
        deviceModel.setStatus(DeviceModelConstant.STATUS_VALID);
        deviceModelMapper.insertSelective(deviceModel);
        operationLogService.saveSuccessOperationLog("新增终端机型：" + deviceModel.getDeviceModel(),
                ModuleConstant.MODULE_DEVICE_MODEL_MNG, request);
    }

    /**
     * 批量删除终端型号
     *
     * @param deviceModelIds 终端型号Id(多个Id之间用逗号隔开 )
     * @param request        请求
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDeviceModelBatch(String deviceModelIds, HttpServletRequest request) throws ServiceException {
        String splitChar = ",";
        StringBuilder deviceModelNamesBuilder = new StringBuilder();
        for (String deviceModelId : deviceModelIds.split(splitChar)) {
            if (StringUtils.isNotBlank(deviceModelId)) {
                Long id = Long.valueOf(deviceModelId);
                DeviceModel deviceModel = deviceModelMapper.selectByPrimaryKey(id);
                if (null == deviceModel) {
                    throw new ServiceException("ID为" + id + "的机型已被删除");
                }
                int count = deviceModelMapper.countDeviceByDeviceModelId(deviceModelId);
                if (count > 0) {
                    //已被终端关联，不能删除
                    throw new ServiceException("机型" + deviceModel.getDeviceModel() + "已被终端关联，请先解除关联");
                }
                deviceModelNamesBuilder.append(",").append(deviceModel.getDeviceModel());
                deviceModelMapper.deleteByPrimaryKey(id);
            }
        }
        String deviceModelNames = deviceModelNamesBuilder.toString();
        if (StringUtils.isNotBlank(deviceModelNames)) {
            deviceModelNames = deviceModelNames.substring(1);
        }
        operationLogService.saveSuccessOperationLog("删除终端机型：" + deviceModelNames,
                ModuleConstant.MODULE_DEVICE_MODEL_MNG, request);
    }

    /**
     * 修改机型
     *
     * @param deviceModel 机型
     * @param request     请求
     * @modify 去掉唯一性验证
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateByPrimaryKeySelective(DeviceModel deviceModel, HttpServletRequest request) throws ServiceException {
//        DeviceModel deviceModelById = deviceModelMapper.selectByPrimaryKey(deviceModel.getDeviceModelId());
////        if (!deviceModel.getDeviceModel().equals(deviceModelById.getDeviceModel())) {
////            // 重复性验证
////            String modelName = deviceModel.getDeviceModel();
////            DeviceModel deviceModelRepeat = deviceModelMapper.getDeviceModelByName(modelName);
////            if (deviceModelRepeat != null) {
////                throw new ServiceException(DeviceModelConstant.MSG_DEVICE_MODEL_EXITS);
////            }
////        }
        deviceModelMapper.updateByPrimaryKeySelective(deviceModel);
        operationLogService.saveSuccessOperationLog("修改终端机型：" + deviceModel.getDeviceModel(),
                ModuleConstant.MODULE_DEVICE_MODEL_MNG, request);
    }

    /**
     * 根据主键查询终端机型
     *
     * @param deviceModelId 主键
     * @return DeviceModel
     */
    @Override
    public DeviceModel getDeviceModelById(Long deviceModelId) {
        return deviceModelMapper.selectByPrimaryKey(deviceModelId);
    }

    /**
     * 查询下拉集合
     *
     * @return List<DeviceModelSelectListVO>
     */
    @Override
    public List<DeviceModelSelectListVO> selectList() {
        return deviceModelMapper.selectList();
    }

    @Override
    public List<DeviceModelDTO> listDeviceModelsSelectedUpgrade(Long deviceUpgradeId) {
        return deviceModelMapper.listDeviceModelsSelectedUpgrade(deviceUpgradeId);
    }

    /**
     * 获取终端型号列表
     * hanqr 2018-08-14
     *
     * @return
     */
    @Override
    public List<DeviceModelDTO> listDeviceModels() {
        return deviceModelMapper.listDeviceModels();
    }

    /**
     * 终端机型导出列表
     *
     * @return List<DeviceModelExportListVO>
     */
    @Override
    public List<DeviceModelExportListVO> listExport(String deviceModel,String mainboard,String brand,Boolean isExits4G) {
        return deviceModelMapper.listExport(deviceModel, mainboard, brand,isExits4G);
    }
}
